package com.mofang.activity.user;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mofang.R;
import com.mofang.activity.BaseTitleViewActiviey;
import com.pactera.framework.util.ToastHelper;

public class CustomerRegistrationDetailActivity extends BaseTitleViewActiviey
		implements OnClickListener {

	private static final int code_select_phone = 0;
	private static final int code_select_area = 1;
	private EditText edit_registration_recommended_detail_name;// 被推荐人
	private EditText edit_registration_recommended_detail_phone;// 被推荐人电话
	private EditText intentionCity_edit;// 意向城市
	private TextView intentionDistrict_text;// 意向区域
	private EditText intentionBuilding_edit;// 意向楼盘
	private EditText intentionArea_edit;// 意向面积
	private EditText intentionTotalPrice_edit;// 意向总价
	private EditText specialRequirement_edit;// 特殊需求
	private LinearLayout registration_detail_ly;// 推荐按钮
	private LinearLayout intentionDistrict_ly;// 意向区域

	private ImageView intentionDistrict_img;
	private Button btn_registration_recommended_detail_phone_select;
	private Button btn_customer_registration_change;// 推荐按钮文字

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_customer_registration_detail);
		super.onCreate(savedInstanceState);
		setNavigationTitle("客户登记本");
		setNavigationLeftBtnImage(R.drawable.return_arrow);
	}

	@Override
	protected void initView() {
		super.initView();
		edit_registration_recommended_detail_name = (EditText) findViewById(R.id.edit_registration_recommended_detail_name);
		edit_registration_recommended_detail_phone = (EditText) findViewById(R.id.edit_registration_recommended_detail_phone);
		btn_registration_recommended_detail_phone_select = (Button) findViewById(R.id.btn_registration_recommended_detail_phone_select);

		intentionCity_edit = (EditText) findViewById(R.id.registration_detail_intentionCity_edit);

		intentionDistrict_text = (TextView) findViewById(R.id.registration_detail_intentionDistrict_text);

		intentionDistrict_ly = (LinearLayout) findViewById(R.id.registration_detail_intentionDistrict_ly);
		intentionDistrict_ly.setOnClickListener(this);

		intentionBuilding_edit = (EditText) findViewById(R.id.registration_detail_intentionBuilding_edit);

		intentionArea_edit = (EditText) findViewById(R.id.registration_detail_intentionArea_edit);

		intentionTotalPrice_edit = (EditText) findViewById(R.id.registration_detail_intentionTotalPrice_edit);

		specialRequirement_edit = (EditText) findViewById(R.id.registration_detail_specialRequirement_edit);

		registration_detail_ly = (LinearLayout) findViewById(R.id.registration_detail_ly);
		registration_detail_ly.setOnClickListener(this);

		intentionDistrict_img = (ImageView) findViewById(R.id.registration_detail_intentionDistrict_img);

		btn_customer_registration_change = (Button) findViewById(R.id.btn_customer_registration_change);

		edit_registration_recommended_detail_name.setText("王建军");
		edit_registration_recommended_detail_phone.setText("12345678901");
		intentionCity_edit.setText("无锡");
		intentionDistrict_text.setText("新区");
		intentionBuilding_edit.setText("朗诗天萃");
		intentionArea_edit.setText("120平方米");
		intentionTotalPrice_edit.setText("120万");
		specialRequirement_edit.setText("卧室朝阳.采光好");
	}

	@Override
	protected void setListener() {
		super.setListener();
		btn_registration_recommended_detail_phone_select
				.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.registration_detail_ly:

			break;
		case R.id.registration_detail_intentionDistrict_ly:

			intent = new Intent(getApplicationContext(),
					AreaSelectActivity.class);
			startActivityForResult(intent, code_select_area);
			break;
		case R.id.btn_registration_recommended_detail_phone_select:
			intent = new Intent(Intent.ACTION_PICK,
					ContactsContract.Contacts.CONTENT_URI);
			startActivityForResult(intent, code_select_phone);
			break;
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case code_select_phone:
			if (resultCode == RESULT_OK) {
				Uri contactData = data.getData();
				Cursor cursor = managedQuery(contactData, null, null, null,
						null);
				cursor.moveToFirst();
				String num = this.getContactPhone(cursor);
				edit_registration_recommended_detail_phone.setText("" + num);
			}
			break;
		case code_select_area:
			if (resultCode == RESULT_OK) {
				Bundle b = data.getExtras(); // data为B中回传的Intent
				int id = b.getInt("id");// str即为回传的值
				String name = b.getString("name");// str即为回传的值
				ToastHelper.getInstance().showLongMsg(name);
			}
			break;
		default:
			break;
		}
	}

	private String getContactPhone(Cursor cursor) {
		// TODO Auto-generated method stub
		int phoneColumn = cursor
				.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
		int phoneNum = cursor.getInt(phoneColumn);
		String result = "";
		if (phoneNum > 0) {
			// 获得联系人的ID号
			int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
			String contactId = cursor.getString(idColumn);
			// 获得联系人电话的cursor
			Cursor phone = getContentResolver().query(
					ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
					null,
					ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
							+ contactId, null, null);
			if (phone.moveToFirst()) {
				for (; !phone.isAfterLast(); phone.moveToNext()) {
					int index = phone
							.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
					int typeindex = phone
							.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
					int phone_type = phone.getInt(typeindex);
					String phoneNumber = phone.getString(index);
					result = phoneNumber;
					// switch (phone_type) {//此处请看下方注释
					// case 2:
					// result = phoneNumber;
					// break;
					//
					// default:
					// break;
					// }
				}
				if (!phone.isClosed()) {
					phone.close();
				}
			}
		}
		return result;
	}
}
